<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>会员信息列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/Swiper/3.4.2/js/swiper.jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/Swiper/3.4.2/css/swiper.min.css">
    <script src="${pageContext.request.contextPath}/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/xadmin.js"></script>
</head>
<body>
<!-- 顶部开始 -->
<div class="container">
    <div class="logo"><a href="./index.html">健身房管理系统 V1.1</a></div>
    <div class="open-nav"><i class="iconfont">&#xe699;</i></div>
    <ul class="layui-nav right" lay-filter="">
        <li class="layui-nav-item">
            <a href="javascript:;">管理员</a>
            <dl class="layui-nav-child">
                <dd><a href="${pageContext.request.contextPath}/member/outlogin.action">退出</a></dd>
            </dl>
        </li>
        <li class="layui-nav-item"><a href="${pageContext.request.contextPath}/index.jsp">前台首页</a></li>
    </ul>
</div>
<!-- 顶部结束 -->
<!-- 中部开始 -->
<div class="wrapper">
    <!-- 左侧菜单开始 -->
    <div class="left-nav">
        <div id="side-nav">
            <jsp:include page="left.jsp"/>
        </div>
    </div>
    <!-- 左侧菜单结束 -->
    <!-- 右侧主体开始 -->
    <div class="page-content">
        <div class="content"  >
            <!-- 右侧内容框架，更改从这里开始 -->
            <form class="layui-form xbs" action="" >
                <div class="layui-form-pane" style="text-align: center;">
                    <div class="layui-form-item" style="display: inline-block;">
                        <label class="layui-form-label xbs768">到期范围:</label>
                        <div class="layui-input-inline xbs768">
                            <input type="date" class="layui-input" placeholder="开会员最早时间" id="min" autocomplete="off">
                        </div>
                        <div class="layui-input-inline xbs768">
                            <input type="date" class="layui-input" placeholder="开会员最晚时间" id="max" autocomplete="off">
                        </div>
                        <div class="layui-input-inline">
                            <input type="text" id="username"  placeholder="请输入用户名" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-input-inline" style="width:80px">
                            <button type="button" class="layui-btn"  lay-submit="" lay-filter="" onclick="findMem()"><i class="layui-icon">&#xe615;</i></button>
                        </div>
                    </div>
                </div>
            </form>
            <div id="table">
                <xblock>
                    <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon">&#xe640;</i>批量删除</button>
                    <button class="layui-btn" onclick="x_admin_show('添加用户','${pageContext.request.contextPath}/adminMem_add.jsp','600','550')"><i class="layui-icon">&#xe608;</i>添加</button>
                    <span class="x-right" style="line-height:40px">共有数据:${memInfo.list.size()}条</span>
                </xblock>
                <table class="layui-table" >
                    <thead>
                    <tr>
                        <th>
                            <input type="checkbox" name="allck" value="">
                        </th>
                        <th>ID</th>
                        <th>真实名字</th>
                        <th>性别</th>
                        <th>联系电话</th>
                        <th>邮箱</th>
                        <th>会员卡</th>
                        <th>开卡时间</th>
                        <th>到期时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach items="${memInfo.list}" var="member">
                        <tr>
                            <td>
                                <input type="checkbox" value="${member.memid}" name="ck">
                            </td>
                            <td>${member.memid}</td>
                            <td>${member.memrealname}</td>
                            <td>${member.memsex}</td>
                            <td>${member.memlogname}</td>
                            <td>${member.mememail}</td>
                            <td>${member.card.cardname}</td>
                            <td><fmt:formatDate value="${member.cardstart}" pattern="yyyy.MM.dd"/></td>
                            <td><fmt:formatDate value="${member.cardend}" pattern="yyyy.MM.dd"/></td>
                            <td class="td-manage">
                                <a title="编辑" href="javascript:;" onclick="editMem(${member.memid})" class="ml-5" style="text-decoration:none">
                                    <i class="layui-icon">&#xe642;</i>
                                </a>
                                &nbsp;&nbsp;
                                <a title="删除" href="javascript:;" onclick="member_del(${member.memid})"
                                   style="text-decoration:none">
                                    <i class="layui-icon">&#xe640;</i>
                                </a>
                            </td>
                        </tr>
                    </c:forEach>

                    </tbody>
                </table>
                <%--            分页--%>
                <div id="bottom">
                    <div>
                        <nav aria-label="..." style="text-align:center;">
                            <ul class="pagination layui-laypage" style="white-space: nowrap">
                                <%--                            向前翻页--%>
                                <li style="display: inline-block"><a href="javascript:ajaxsplit(${memInfo.prePage})" aria-label="Previous">&laquo;</a></li>

                                <c:forEach begin="1" end="${memInfo.pages}" var="i">
                                    <c:if test="${memInfo.pageNum==i}">
                                        <li style="display: inline-block">
                                            <a href="javascript:ajaxsplit(${i})"
                                               style="background-color: #DFDFDF">${i}</a>
                                        </li>
                                    </c:if>
                                    <c:if test="${memInfo.pageNum!=i}">
                                        <li style="display: inline-block">
                                            <a href="javascript:ajaxsplit(${i})">${i}</a>
                                        </li>
                                    </c:if>
                                </c:forEach>
                                <%--                            向后翻页--%>
                                <li style="display: inline-block"><a href="javascript:ajaxsplit(${memInfo.nextPage})" aria-label="Previous">&raquo;</a></li>
                            </ul>
                        </nav>
                    </div>
                </div>
            </div>

            <!-- 右侧内容框架，更改从这里结束 -->
        </div>
    </div>
    <!-- 右侧主体结束 -->
</div>
<!-- 中部结束 -->
<!-- 页面动态效果 -->
<script>
    //分页
    function ajaxsplit(page) {
        //判断输入框是否为空，为空则普通Ajax分页查询。不为空则带条件的Ajax。
        var a1 = $("#min").val();
        var a2 = $("#max").val();
        var a3 = $("#username").val();
        if (a1.length==0 && a2.length==0 && a3.length==0){
            //不带条件异步ajax分页请求
            $.ajax({
                url:"${pageContext.request.contextPath}/admin/ajaxsplit.action",
                data:{"page":page},
                type:"post",
                success:function () {
                    //更新id=table部分内容
                    $("#table").load("http://localhost:8080/GamnasiumSystem/adminMem_list.jsp #table");
                }
            });
        }else{
            //带条件异步Ajax分页请求
            $.ajax({
                url:"${pageContext.request.contextPath}/admin/adminFindMemSplit.action",
                data:{
                    page:page,
                    datemin:a1,
                    datemax:a2,
                    memname:a3
                },
                type:"post",
                success:function () {
                    //更新id=table部分内容
                    $("#table").load("http://localhost:8080/GamnasiumSystem/adminMem_list.jsp #table");
                }
            });
        }

    }
    //多条件搜索，用Ajax发送请求,然后更新session=memInfo内容
    function findMem(){
        $.ajax({
            url: "${pageContext.request.contextPath}/admin/adminFindMem.action",
            type: "post",
            data: {
                datemin:$("#min").val(),
                datemax:$("#max").val(),
                memname:$("#username").val()
            },
            success:function (){
                //更新id=table部分内容
                $("#table").load("http://localhost:8080/GamnasiumSystem/adminMem_list.jsp #table");
            },
            error:function (){
                console.log("error");
            }
        });
    }
    //编辑用户信息
    function editMem(index){
        console.log(index);
        $.ajax({
            url: "${pageContext.request.contextPath}/admin/editShow.action",
            type: "post",
            data: {
                memid:index
            },
            success:function (){
                console.log("result");
                //查到要修改的人就弹出
                x_admin_show('编辑','${pageContext.request.contextPath}/adminMem-edit.jsp','','550');
            },
            error:function (){
                console.log("error");
            }
        });
    }
    /*用户-删除*/
    function member_del(obj){
        console.log(obj);
        layer.confirm('确认要删除吗？',function(){
            //发异步删除数据
            $.ajax({
                url: "${pageContext.request.contextPath}/admin/editByStates0.action",
                type: "post",
                data: {memid:obj},
                success:function (result){
                    if (result=="ok"){
                        window.top.location.href="http://localhost:8080/GamnasiumSystem/admin/splitOnepage.action";
                    }else {
                        layer.msg('删除失败，请重试！',{icon:1});
                    }
                },
                error:function (){
                    console.log("error");
                }
            });
        });
    }
    //全选按钮
    $("input[name='allck']").click(function (){
        //全选
        var allck = $("input[name='allck']");
        //子复选框
        var ck = $("input[name='ck']");
        // console.log("ck:"+ck);
        if (allck.prop("checked")){
            //1.全选
            ck.prop("checked",true);
        }else {
            //2.反选
            ck.prop("checked",false);
        }
    });
    //3.子复选框非全选
    $("input[name='ck']").click(function (){
        //子复选框长度
        var ck = $("input[name='ck']");
        //全选
        var allck = $("input[name='allck']");
        //获取选中的子ck
        var cked = $("input:checkbox[name='ck']:checked");
        // console.log("ck:"+ck);
        // console.log(" cked:"+cked[0].value);
        if (cked.length!=ck.length){
            allck.prop("checked",false);
        }else {
            allck.prop("checked",true);
        }
    });
    //批量删除提交
    function delAll () {
        layer.confirm('确认要删除吗？',function(){
            //捉到所有被选中的，发异步进行删除
            var cked = $("input:checkbox[name='ck']:checked");
            // console.log(cked[1].value);
            var memids = [];
            cked.each(function (){
                memids.push($(this).val());
                // console.log($(this).val());
            });
            $.ajax({
                url: "${pageContext.request.contextPath}/admin/delBatch0.action",
                type: "post",
                data: {
                    //传字符串到后端
                    memids:memids.toString()
                },
                success:function (result){
                    console.log(result);
                    if (result=="ok"){
                        window.top.location.href="http://localhost:8080/GamnasiumSystem/admin/splitOnepage.action";
                    }else {
                        layer.msg('删除失败，请重试！',{icon:1});
                    }
                },
                error:function (){
                    console.log("error");
                }
            });
            layer.msg('删除成功', {icon: 1});
        });
    }
</script>
</body>
</html>
